package com.xiaomaoguai.annotation.anno;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.TypeVariable;

import org.apache.commons.lang3.StringUtils;

/**
 * @Description :
 * @FileName: DAOImplement.java
 * @Author :WeiHui.Zhang
 * @Data : 2015年6月5日 下午3:18:31
 * @Version:V1.00
 */
public class DAOImplement implements InvocationHandler {

	@Override
	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

		if (method.isAnnotationPresent(SQLContent.class)) {

			System.out.println(method.getName());

			TypeVariable<Method>[] typeParameters = method.getTypeParameters();

			for (TypeVariable<Method> typeVariable : typeParameters) {
				System.out.println(typeVariable.getName());
			}

			SQLContent content = method.getAnnotation(SQLContent.class);

			System.out.println(content.value());

		}

		if (args != null) {
			for (int i = 0; i < args.length; i++) {
				Annotation[] annotations = method.getParameterAnnotations()[i];
				if (annotations.length == 0) {
					System.out.println("第" + i + "个参数并没有注解");
				} else {
					for (Annotation a : annotations) {
						if (a.annotationType().equals(SQLParam.class)) {
							SQLParam sqlParamAnn = (SQLParam) a;
							System.out.println(sqlParamAnn.value());
						}
					}
				}
			}
		}

		Class<?> returnType = method.getReturnType();

		Object data = returnType.newInstance();

		Field[] fields = returnType.getFields();
		for (Field field : fields) {
			System.out.println(field.getName());
		}

		Field[] declaredFields = data.getClass().getDeclaredFields();
		for (Field field : declaredFields) {
			System.out.println(field.getName());
			field.setAccessible(true);
		}
		Method[] declaredMethods = data.getClass().getDeclaredMethods();
		for (Method method2 : declaredMethods) {
			System.out.println(method2);
			method2.setAccessible(true);
		}

	Class<?> class1 = data.getClass();

	Field field = class1.getField("id");
	Field field2 = class1.getField("name");
	field.set("id", 6);
	field2.set("name", "zhang");
		return data;
	}

    public static void setter(Object obj, String fieldName, Object newVal){
        String setterName = "set"+ StringUtils.capitalize(fieldName);
        Class<?> target = obj.getClass();
        try {
            Method setterMethod = target.getMethod(setterName);
            try {
                setterMethod.invoke(obj, newVal);
            } catch (Exception e) {
                throw new RuntimeException("Cannot invoke setter method:"+target.getCanonicalName() + "." + setterMethod.getName(),e);
            }
        } catch (NoSuchMethodException e) {
            try {
                Field field = target.getField(fieldName);
                try {
                    field.set(obj, newVal);
                } catch (IllegalAccessException e1) {
                    throw new RuntimeException("Cannot set value to field:"+target.getCanonicalName() + "." + fieldName,e1);
                }
            } catch (NoSuchFieldException e1) {
                throw new RuntimeException("No such field:"+target.getCanonicalName() + "." + fieldName,e1);
            }
        }
    }

}
